//
// Created by ttao on 2022/8/4.
//
#ifndef HTTPSERVER_CONTAINER_H
#define HTTPSERVER_CONTAINER_H
/* ================ start of Stack.c ================ */
#include "atomicvar.h"
#include "lock.h"
#include "dts.h"
typedef struct {
    char **head;            // 栈首地址
    long point;         // 栈顶计数器
    long maxlen;        // 栈长度
} stack;

static inline stack *initStack(stack *s, char **head, long MAXSIZE)
{
    s->head = head;
    s->maxlen = MAXSIZE;
    s->point = 0;
}

void stack_clear(stack *s);
int push(stack *s, void *node);
void *pop(stack *s);
int isempty(stack *s);
/* ================ end of Stack.c ================ */


/* ================ end of Lists.c ================ */
enum InsertType{
    BEFORE,
    AFTER,
};
struct Table{
    struct Table *prev;
    struct Table *next;
    char *value;
} ;
struct LinkList{
    struct Table* head;
    struct Table* tail;
    struct Table* cursor;   //游标
    long size;
    pthread_rwlock_t    rwlock;
    pthread_mutex_t     lock;
    pthread_cond_t      cont;
};
typedef struct Table table_t;
typedef struct LinkList linklist_t;

linklist_t* initLinkList(linklist_t *list);
void add_linklist_before(linklist_t *list, void *val);      // 追加数据 前面插入
void add_linklist_after(linklist_t *list, void *val);       // 追加数据 后面插入
void ready_linklist_data(linklist_t *list);                 // 元素遍历前一定要调用
int has_linklist_next(linklist_t *list);
table_t *get_linklist(linklist_t *list);
int destry_linklist(linklist_t *list, table_t *tb);                    // 销毁元素
/* ================ start of Lists.c ================ */

/* ================ start of Queue.c ================ */
typedef struct queue_task_s queue_task_t;
struct queue_task_s{
    queue_task_t *next;
    void *task;
};

struct Queue{
    queue_task_t        *first;
    queue_task_t       **last;
    long                size;
    long                maxsize;
    long                lock_count;
    pthread_rwlock_t    rwlock;
    pthread_mutex_t     lock;
    pthread_cond_t      cont;
};
struct Queue *initQueue(struct Queue *queue, int MAXSIZE);
int task_push(struct Queue *queue, void *task);
void* task_pop(struct Queue *queue);
void push_task_block(struct Queue *queue, void *task);
void *pop_task_block(struct Queue *queue);
char* stream_pop_queue(struct Queue *queue, char *desc, int size); //获取队列数据
void stream_push_queue(struct Queue *queue, dts d, int len);  // 以流的方式入队
int get_queue_size(struct Queue *queue);
int has_queue_next(struct Queue *queue);
/* ================ end of Queue.c ================ */
#endif //HTTPSERVER_CONTAINER_H
